import 'package:deer/res/colors.dart';
import 'package:deer/res/styles.dart';
import 'package:deer/util/number_text_input_formatter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:flutter/foundation.dart';

class TextFieldItem extends StatelessWidget {
  const TextFieldItem(
      {Key key,
      @required this.title,
      this.hintText: '',
      this.keyboardType: TextInputType.text,
      this.config,
      this.height: 50,
      this.controller,
      this.focusNode,
      this.textStyle: TextStyles.textDark14,
      this.hintStyle: TextStyles.textGrayC14})
      : super(key: key);

  final String title;
  final String hintText;
  final TextInputType keyboardType;
  final KeyboardActionsConfig config;
  final double height;
  final TextEditingController controller;
  final FocusNode focusNode;
  final TextStyle textStyle;
  final TextStyle hintStyle;

  @override
  Widget build(BuildContext context) {
    if (config != null && defaultTargetPlatform == TargetPlatform.iOS) {
      // 因Android平台输入法兼容问题，所以只配置IOS平台
      FormKeyboardActions.setKeyboardActions(context, config);
    }
    return Container(
      height: height,
      margin: const EdgeInsets.only(left: 16),
      width: double.infinity,
      decoration: BoxDecoration(
          border: Border(
        bottom:
            Divider.createBorderSide(context, color: Colours.line, width: 0.6),
      )),
      child: Row(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(right: 16),
            child: Text(
              title ?? '',
              style: TextStyles.textDark14,
            ),
          ),
          Expanded(
            flex: 1,
            child: TextField(
              inputFormatters: _getInputFormatters(),
              controller: controller,
              focusNode: focusNode,
              textInputAction: TextInputAction.done,
              keyboardType: keyboardType,
              style: textStyle,
              decoration: InputDecoration(
                  hintText: hintText ?? '',
                  border: InputBorder.none,
                  hintStyle: hintStyle),
            ),
          )
        ],
      ),
    );
  }

  _getInputFormatters() {
    if (keyboardType == TextInputType.numberWithOptions(decimal: true)) {
      return [UsNumberTextInputFormatter()];
    }
    if (keyboardType == TextInputType.number ||
        keyboardType == TextInputType.phone) {
      return [WhitelistingTextInputFormatter.digitsOnly];
    }
    return null;
  }
}
